﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

namespace CJJCH.Web
{
    public partial class OnLineRegis : System.Web.UI.Page
    {
        CJJCH.BLL.T_NET_MenuSecond bll_second = new CJJCH.BLL.T_NET_MenuSecond();
        CJJCH.BLL.T_NET_MenuThird bll_third = new CJJCH.BLL.T_NET_MenuThird();
        CJJCH.BLL.T_NET_OLType bll_oltype = new CJJCH.BLL.T_NET_OLType();
        CJJCH.BLL.T_NET_OLItem bll_olitem = new CJJCH.BLL.T_NET_OLItem();
        CJJCH.BLL.T_NET_Online bll_online = new BLL.T_NET_Online();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitRepater();
                InitControl();
            }
            LTP.Common.ButtonClass.ButtonOnClick(this.Page,btnSubmit,"checkform()",false);
        }
        private void InitRepater()
        {
            if (Request.QueryString["firstid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["firstid"].Trim()))
            {
                lblFID.Text = Request.QueryString["firstid"].ToString();
                Top1.IIndex = Convert.ToInt32(lblFID.Text);
                Top1.FirstId = Convert.ToInt32(lblFID.Text);

            }
            else
            {
                Response.Redirect("error.aspx");
            }
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["firstid"].Trim()))
            {
                lblSID.Text = Request.QueryString["secondid"].ToString();
                Top1.SecondId = Convert.ToInt32(lblSID.Text);
            }
            else
            {
                Response.Redirect("error.aspx");
            }
            if (Request.QueryString["thirdid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["firstid"].Trim()))
            {
                Top1.ThirdId = Convert.ToInt32(Request.QueryString["thirdid"]);
            }
           
            if (Request.QueryString["keyword"] != null)
            {
                lblKeyWord.Text = Request.QueryString["keyword"].ToString();
                Top1.Keyword = lblKeyWord.Text;

            }
            if (Request.QueryString["typename"] != null)
            {

                lbltypeName.Text = Request.QueryString["typeName"].Trim();
                Top1.Typename = lbltypeName.Text;

            }


        }

        private void InitControl()
        {
            CJJCH.Model.T_NET_OLType model = new CJJCH.Model.T_NET_OLType();
            model = bll_oltype.GetOLTypeModel();
            if (model!=null)
	        {
                IList<CJJCH.Model.T_NET_OLItem> list = new List<CJJCH.Model.T_NET_OLItem>(); 
                list= bll_olitem.GetOLItem(model.ID);
                if (list.Count>0)
                {
                    this.dropItem.DataSource = list;
                    this.dropItem.DataValueField = "ID";
                    this.dropItem.DataTextField = "ItemName";
                    this.dropItem.DataBind();
                }
                this.dropItem.Items.Insert(0,new ListItem("请选择",""));
	        }
            else
            {
                this.dropItem.Items.Insert(0, new ListItem("请选择", ""));
                this.lblMessage.Style.Add("display","inline");
                this.divShow.Style.Add("display", "none");
            }
        }

        protected string checkInput()
        {
            StringBuilder error = new StringBuilder();

            if (this.txtCompanyName.Text.Trim() == "")
            {
                error.Append("请输入单位名称！\\n");
            }
            if (this.txtCompanyAddress.Text.Trim() == "")
            {
                error.Append("请输入单位地址！\\n");
            }
            if (this.txtPostCode.Text == "")
            {
                error.Append("请输入邮编！\\n");
            }
            else if (!LTP.Common.CheckInput.CheckPostcode(this.txtPostCode.Text.Trim()))
            {
                error.Append("请输入正确的邮编！\\n");
            }
            if (this.txtContactName.Text.Trim() == "")
            {
                error.Append("请输入联系人姓名！\\n");
            }
            if (this.txtUserMobile.Text == "")
            {
                error.Append("请输入手机号码！\\n");
            }
            else if (!LTP.Common.CheckInput.CheckMobile(this.txtUserMobile.Text.Trim()))
            {
                error.Append("请输入正确的手机号码！\\n");
            }
            if (this.txtUserEmail.Text == "")
            {
                error.Append("请输入e-mail地址！\\n");
            }
            else if (!LTP.Common.CheckInput.CheckEmail(this.txtUserEmail.Text.Trim()))
            {
                error.Append("请输入正确的e-mail地址！\\n");
            }
            if (this.dropItem.SelectedValue == "")
            {
                error.Append("请选择报名讲座主题！\\n");
            }
            if (!LTP.Common.CheckInput.CheckGUID(this.dropItem.SelectedValue))
            {
                error.Append("请选择正确的报名讲座主题！\\n");
            }
            if (this.txtPeopleCount.Text == "")
            {
                error.Append("请输入报名人数！\\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(this.txtPeopleCount.Text.Trim()))
            {
                error.Append("请输入正确的报名人数！\\n");
            }
            return error.ToString();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string error = checkInput();
            if (string.IsNullOrEmpty(error))
            {
                CJJCH.Model.T_NET_Online model = new Model.T_NET_Online();
                model.BuildDate = DateTime.Now;
                model.CompanyAddress = LTP.Common.StringClass.EncodeString(this.txtCompanyAddress.Text.Trim());
                model.CompanyName = LTP.Common.StringClass.EncodeString(this.txtCompanyName.Text.Trim());
                model.ContactName = LTP.Common.StringClass.EncodeString(this.txtContactName.Text.Trim());
                model.DutyName = LTP.Common.StringClass.EncodeString(this.txtDutyName.Text.Trim());
                model.FirstID = int.Parse(this.lblFID.Text.Trim());
                model.ID = Guid.NewGuid();
                model.ItemID = new Guid(LTP.Common.StringClass.EncodeString(this.dropItem.SelectedValue));
                model.OfficeTel = LTP.Common.StringClass.EncodeString(this.txtOfficeTel.Text.Trim());
                model.PeopleCount = int.Parse(this.txtPeopleCount.Text.Trim());
                model.PostCode = LTP.Common.StringClass.EncodeString(this.txtPostCode.Text.Trim());
                model.SecondID = int.Parse(this.lblSID.Text.Trim());
                model.ThirdID = 0;
                model.UserEmail = LTP.Common.StringClass.EncodeString(this.txtUserEmail.Text.Trim());
                model.UserFax = LTP.Common.StringClass.EncodeString(this.txtUserFax.Text.Trim());
                model.UserMobile = LTP.Common.StringClass.EncodeString(this.txtUserMobile.Text.Trim());
                string res = bll_online.AddOnline(model);
                if (res=="")
	            {
                    LTP.Common.MessageBox.ShowAndRedirect(this, "报名成功！", "OnLineRegis.aspx?firstid=3&secondid=8&thirdid=0");
	            }
                else
	            {
                    LTP.Common.WindowClass.WindowBack(res);
	            }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(error);
            }
        }

    }
}